草庐IT

C++ ifstream 到 char *

全部标签

c++ - 在调用 toupper()、tolower() 等之前,我是否需要转换为 unsigned char?

不久前,在StackOverflow上享有盛誉的人在评论中写道,在调用之前有必要将char参数转换为unsignedchar>std::toupper和std::tolower(以及类似的函数)。另一方面,BjarneStroustrup在C++编程语言中没有提到这样做的必要性。他只是使用toupper之类的stringname="NielsStroustrup";voidm3(){strings=name.substr(6,10);//s="Stroustrup"name.replace(0,5,"nicholas");//namebecomes"nicholasStroustrup

c++ - 如何将 vector<unsigned char> 转换为 char*

我有一个这样的缓冲区:vectorbuf如何将其转换为char*?如果我这样做:(char*)buf我收到此错误:/home/richard/Desktop/richard/client/src/main.cc:102:error:invalidcastfromtype‘std::vector>’totype‘char*’对于那些想知道我为什么要这样做的人。我需要将缓冲区传递给这个函数:n_sent=sendto(sk,(char*)buf,(int)size,0,(structsockaddr*)&server,sizeof(server));而且它只接受char*。

c++ - 无法将参数 1 从 'char' 转换为 'LPCWSTR'

我不断收到此错误:无法将参数1从'char'转换为'LPCWSTR'intmain(intargc,charargv[]){//openportforI/OHANDLEh=CreateFile(argv[1],GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);if(h==INVALID_HANDLE_VALUE){PrintError("E012_Failedtoopenport");有人可以帮忙吗? 最佳答案 转到项目的属性并在配置属性/常规下,将字符集更改为“未设置”

c++ - 从 (char*, size_t) 创建 C++ 内存流的更简单方法,无需复制数据?

我找不到任何现成的,所以我想出了:classmembuf:publicbasic_streambuf{public:membuf(char*p,size_tn){setg(p,p,p+n);setp(p,p+n);}}用法:char*mybuffer;size_tlength;//...allocate"mybuffer",putdataintoit,set"length"membufmb(mybuffer,length);istreamreader(&mb);//use"reader"我知道stringstream,但它似乎无法处理给定长度的二进制数据。我是在发明自己的轮子吗?编辑它

C++11 引入了采用 `const char*` 的异常构造函数。但为什么?

Standardlibrarydefect#254其中包括添加新的异常构造函数:std::logic_error::logic_error(constchar*what_arg);std::runtime_error::runtime_error(constchar*what_arg);//etc.给出了这样一种想法,即存储std::string会打开一些与潜在的内存分配问题有关的蠕虫。但是,followinginitiationofadiscussionbyorlpintheLounge,令我震惊的是,除非标准要求what_arg只是一个字符串文字(或指向其他静态存储持续时间缓冲区的

c++ - 警告 : ISO C++ forbids converting a string constant to ‘char*’ for a static `constexpr char*` data member

这个问题在这里已经有了答案:constexprconstvsconstexprvariables?(3个回答)关闭3年前。为什么这段代码会返回警告warning:ISOC++forbidsconvertingastringconstantto‘char*’[-Wwrite-strings]如果Aconstexprspecifierusedinanobjectdeclarationornon-staticmemberfunction(untilC++14)impliesconst.Aconstexprspecifierusedinafunctionorstaticmembervariab

c++ - 为什么 c++ 中 char 和 bool 的大小相同?

我正在阅读C++编程语言。Stroustrup在其中指出sizeof(char)==1和1.具体取决于实现。为什么像boolean值这样简单的值会占用与字符相同的空间? 最佳答案 在现代计算机体系结构中,字节是最小的可寻址内存单元。要将多个位打包到一个字节中,需要应用额外的位移操作。在编译器级别,这是内存与速度要求之间的权衡(在高性能软件中,那些额外的位移操作会不必要地增加和减慢应用程序的速度)。 关于c++-为什么c++中char和bool的大小相同?,我们在StackOverflow

c++ - 从 'const char*' 到 'char*' 的无效转换

有一个如下所示的代码。我在传递参数时遇到问题。stringstreamdata;char*addr=NULL;strcpy(addr,retstring().c_str());retstring()是一个返回字符串的函数。//morecodeprintfunc(num,addr,data.str().c_str());我得到了错误invalidconversionfrom'constchar*'to'char*'.initializingargument3of'voidPrintfunc(int,char*,char*)'onargument3ofthefunction在上面一行。函数

c++ - ifstream : check if opened successfully

一位同事刚刚告诉我这段代码:std::ifstreamstream(filename.c_str());if(!stream){throwstd::runtime_error("..");}会错的。他说如果打开成功,ifstream的计算结果为0。我的代码有效,但我想找到文档,但没有看到它说明如何检查打开是否成功。可以指点一下吗? 最佳答案 运算符!isoverloaded对于std::ifstream,所以你可以这样做。不过,在我看来,这是对运算符重载的可怕滥用(由标准委员会提出)。如果您只执行if(stream.fail()).

c++ - 为什么我会收到此 ifstream 错误?

Implicitinstantiationofundefinedtemplate'std::basic_ifstream>'#ifndefMAPPER_H#defineMAPPER_H#include#include#include#include"KeyValue.h"#include"Parser.h"usingnamespacestd;classMapper{public:Mapper(ifstream&infile);~Mapper(void);voidloadTokens();voidshowTokens();voidmap();voidprintMap();voidprin